package net.begincode.service.blog.impl;

import java.util.ArrayList;
import java.util.List;

import utils.CacheUtils;
import config.DbTemplate;
import net.begincode.bean.BcBlog;
import net.begincode.bean.CourseChapter;
import net.begincode.service.blog.BlogService;

public class BlogServiceImpl implements BlogService {
	private BlogServiceImpl() {
	}

	private static BlogServiceImpl instance = null;

	public static BlogServiceImpl getInstance() {
		if (instance == null) {
			synchronized (BlogServiceImpl.class) {
				if (instance == null) {
					instance = new BlogServiceImpl();
				}
			}
		}
		return instance;
	}

	@Override
	public BcBlog addBlog(BcBlog bcBlog) {
		
		StringBuilder addBlog = new StringBuilder();
		addBlog.append(" insert into bc_blog (bc_user_name,bc_classify_id,blog_title,blog_abstract,blog_content, blog_keyword, ");
		addBlog.append(" wonderfu,site_recommend,praise,bc_user_id,view_count,discuss_flag,check_flag,sts,cdate) ");
		addBlog.append(" values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,'1',now())  ");
		int returnValue = DbTemplate.insert(addBlog.toString(),
				new Object[] { bcBlog.getBc_user_name(),
						bcBlog.getBc_classify_id(), bcBlog.getBlog_title(),
						bcBlog.getBlog_abstract(), bcBlog.getBlog_content(),
						bcBlog.getBlog_keyword(), bcBlog.getWonderfu(),
						bcBlog.getSite_recommend(), bcBlog.getPraise(),
						bcBlog.getBc_user_id(), bcBlog.getView_count(),
						bcBlog.getDiscuss_flag(), bcBlog.getCheck_flag() });
		if(returnValue > 0){
			String selSql = " select * from bc_blog where sts = '1' and bc_user_id = ? order by bc_blog_id desc limit 1 ";
			return DbTemplate.queryForObject(selSql, new Object[]{bcBlog.getBc_user_id()}, BcBlog.class);
		}else{
			return null;
		}
	}

	@Override
	public BcBlog blogShow() {
		
		String selSql = "select bb.*,bu.pic from bc_blog  bb left join bc_user bu on bb.bc_user_id = bu.bc_user_id where date(bb.cdate) = curdate()    order by bb.view_count desc  limit 1";
		return DbTemplate.queryObject(selSql,BcBlog.class);
	}

	@Override
	public List<BcBlog> selBlogLimitN(int n) {
		
		String selSql = " select * from bc_blog order by cdate desc limit ?";
		return DbTemplate.queryForList(selSql, new Object[]{n}, BcBlog.class);
	}

	@Override
	public List<BcBlog> selBlogByParam(BcBlog bcBlog) {
		String selSql = " select bb.*,bc.classify_note from bc_blog  bb left join bc_classify bc on bb.bc_classify_id = bc.bc_classify_id where bb.bc_user_id = ? ";
		return DbTemplate.queryForList(selSql, new Object[]{bcBlog.getBc_user_id()}, BcBlog.class);
	}

	@Override
	public BcBlog selBlogById(int id) {
		
		String selSql = " select bb.*,bc.classify_note from bc_blog  bb left join bc_classify bc on bb.bc_classify_id = bc.bc_classify_id where bb.bc_blog_id = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{id}, BcBlog.class);
	}

	@Override
	public int updBlog(BcBlog bcBlog) {
		ArrayList paraList = new ArrayList();
		StringBuilder updSql = new StringBuilder();
		updSql.append(" update bc_blog set bc_user_name = ? ");
		paraList.add(bcBlog.getBc_user_name());
		if(bcBlog.getBc_classify_id() != 0){
			updSql.append(" ,bc_classify_id  = ? ");
			paraList.add(bcBlog.getBc_classify_id());
		}
		if(bcBlog.getBlog_title() != null){
			updSql.append(" ,blog_title  = ? ");
			paraList.add(bcBlog.getBlog_title());
		}
		if(bcBlog.getBlog_abstract() != null){
			updSql.append(" ,blog_abstract  = ? ");
			paraList.add(bcBlog.getBlog_abstract());
		}
		if(bcBlog.getBlog_content() != null){
			updSql.append(" ,blog_content  = ? ");
			paraList.add(bcBlog.getBlog_content());
		}
		if(bcBlog.getBlog_keyword() != null){
			updSql.append(" ,blog_keyword  = ? ");
			paraList.add(bcBlog.getBlog_keyword());
		}
		if(bcBlog.getWonderfu() != null){
			updSql.append(" ,wonderfu  = ? ");
			paraList.add(bcBlog.getWonderfu());
		}
		if(bcBlog.getSite_recommend() != null){
			updSql.append(" ,site_recommend  = ? ");
			paraList.add(bcBlog.getSite_recommend());
		}
		if(bcBlog.getPraise() != 0){
			updSql.append(" ,praise  = ? ");
			paraList.add(bcBlog.getPraise());
		}
		if(bcBlog.getBc_user_id() != 0){
			updSql.append(" ,bc_user_id  = ? ");
			paraList.add(bcBlog.getBc_user_id());
		}
		if(bcBlog.getView_count() != 0){
			updSql.append(" ,view_count  = ? ");
			paraList.add(bcBlog.getView_count());
		}
		if(bcBlog.getDiscuss_flag() != null){
			updSql.append(" ,discuss_flag  = ? ");
			paraList.add(bcBlog.getDiscuss_flag());
		}
		if(bcBlog.getCheck_flag() != null){
			updSql.append(" ,check_flag  = ? ");
			paraList.add(bcBlog.getCheck_flag());
		}
		if(bcBlog.getSts() != null){
			updSql.append(" ,sts  = ? ");
			paraList.add(bcBlog.getSts());
		}
		updSql.append(" where bc_blog_id = ? ");
		paraList.add(bcBlog.getBc_blog_id());
		return DbTemplate.update(updSql.toString(), paraList.toArray());
	}

	@Override
	public List<BcBlog> selBlogLimitN(int userId, int n) {
		
		String selSql = " select * from bc_blog where bc_user_id = ? order by view_count desc limit ?";
		return DbTemplate.queryForList(selSql, new Object[]{userId,n}, BcBlog.class);

	}

	@Override
	public BcBlog selBlogByIdCache(int id) {
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.BLOG_CACHE,"blgId"+id);
		if(dbEle != null){
			return (BcBlog)dbEle;
		}else{
			String selSql = " select bb.*,bc.classify_note from bc_blog  bb left join bc_classify bc on bb.bc_classify_id = bc.bc_classify_id where bb.bc_blog_id = ? ";
			BcBlog ccs = DbTemplate.queryForObject(selSql, new Object[]{id}, BcBlog.class);
			CacheUtils.addEleByParam(CacheUtils.BLOG_CACHE, "blgId"+id, ccs);
			return ccs;
		}
	}

	@Override
	public void updBlogViewCount(String id, int count) {
		String updSql = " update bc_blog set view_count = view_count + ? where bc_blog_id = ? ";
		DbTemplate.update(updSql, new Object[]{count,id});
	}

}
